package com.dq.service.record.impl;

import com.dq.controller.in.WxUserChangeQuery;

import com.dq.domain.record.WxUserChangeRecord;
import com.dq.domain.sys.section.SysSection;
import com.dq.repository.BaseRepositories;
import com.dq.service.record.WxUserChangeRecordService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * @Author luchangtong
 * @Date 2019/12/11 11:49
 */
@Service
public class WxUserChangeRecordServiceImpl extends BaseRepositories implements WxUserChangeRecordService {
    @PersistenceContext
    private EntityManager entityManager;
    @Override
    public List<WxUserChangeRecord> findByWhere(WxUserChangeQuery query) {
        StringBuilder hql = new StringBuilder("select u from WxUserChangeRecord u where u.deleted=false ");
        Query q = createWhereHql(query,hql);
        q.setFirstResult((query.getPage()-1)*query.getSize());
        q.setMaxResults(query.getSize());
        List<WxUserChangeRecord> list = q.getResultList();
        return list;
    }

    @Override
    public Long countByWhere(WxUserChangeQuery query) {
        StringBuilder hql = new StringBuilder("select count(u) from WxUserChangeRecord u where u.deleted=false");
        Query q = createWhereHql(query,hql);
        return new Long((long) q.getResultList().get(0));
    }

    public Query createWhereHql(WxUserChangeQuery query, StringBuilder hql){
        if(query.getOrgId() != null){
            hql.append(" and (u.originalOrg =:sysDept");
            hql.append(" or u.nowOrg =:sysDept)");
        }
        if(query.getWxUserChangeType() != null){
            hql.append(" and u.wxUserChangeType =:wxUserChangeType");
        }
        if(query.getBeginTime() != null){
            hql.append(" and u.createTime >=:beginTime");
        }
        if(query.getEndTime() != null){
            hql.append(" and u.createTime <=:endTime");
        }
        if(StringUtils.isNotEmpty(query.getWxUserName())){
            hql.append(" and u.sysUser.userRealcName like:wxUserName");
        }
        hql.append(" ORDER BY u.createTime desc");
        Query q = entityManager.createQuery(hql.toString());
        if(query.getOrgId() != null){
            SysSection section = dao.get(SysSection.class,query.getOrgId());
            q.setParameter("sysDept", section);
        }
        if(query.getWxUserChangeType() != null){
            q.setParameter("wxUserChangeType",query.getWxUserChangeType());
        }
        if(query.getBeginTime() != null){
            q.setParameter("beginTime",query.getBeginTime());
        }
        if(query.getEndTime() != null){
            q.setParameter("endTime",query.getEndTime());
        }
        if(StringUtils.isNotEmpty(query.getWxUserName())){
            q.setParameter("wxUserName","%"+query.getWxUserName()+"%");
        }
        return q;
    }
}
